package com.starbucks.opencase.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.entity.entPetInfo;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMTip;
import com.starbucks.opencase.enums.enumTip;
import com.starbucks.opencase.function.funCarry;
import com.starbucks.opencase.function.funCommand;
import com.starbucks.opencase.function.funTest;
import com.starbucks.opencase.request.reqAdminCommand;
import com.starbucks.opencase.request.reqData;
import com.starbucks.opencase.response.resAdminPetOptions;
import com.starbucks.opencase.tool.toolAdminAccount;
import com.starbucks.opencase.tool.toolPetInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/pet")
public class conAdminPet {
    @Autowired
    private funCommand funCommand;
    @Autowired
    private funTest funTest;
    @Autowired
    private funCarry funCarry;
    @Autowired
    private toolPetInfo toolPetInfo;
    @Autowired
    private toolAdminAccount toolAdminAccount;
    @PostMapping("/query/options")
    public resAdminPetOptions queryOptions(@RequestBody reqAdminCommand info) {
        resAdminPetOptions res=new resAdminPetOptions();
        if(info.getPhone()==null || info.getPhone().isEmpty()){
            res.setTip(enumTip.no_has_phone.getTip());
            return res;
        }
        if(info.getCommand()==null || info.getCommand().isEmpty()){
            res.setTip(enumTip.no_has_command.getTip());
            return res;
        }
        String[] com= funCommand.decode(info.getCommand());
        if(com==null){
            res.setTip(enumTip.error_command.getTip());
            return res;
        }
        if(!info.getPhone().equals(com[0])){
            res.setTip(enumTip.error_command.getTip());
            return res;
        }
        if(res.getTimestamp()>Long.parseLong(com[1])){
            res.setTip(enumTip.past_que_command.getTip());
            return res;
        }
        res.setData(toolPetInfo.queryOptions());
        res.setStatus(true);
        return res;
    }
    @PostMapping("/query/list")
    public String queryList(@RequestBody reqData info){
        try {
            ObjectMapper om = new ObjectMapper();
            JsonNode me = om.readTree(info.getData());
            ObjectNode on=om.createObjectNode();
            on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
            on.put(enumCMKey.status.name(), false);
            if(!funTest.hasPhone(me,on))return on.toString();
            if(!funTest.hasCommand(me,on))return on.toString();
            String phone= funTest.getPhone(me);
            String command= funTest.getCommand(me);
            if(!toolAdminAccount.queryCommand(phone).equals(command)){
                on.put(enumCMKey.tip.name(), enumCMTip.error_command.getTip());
                return on.toString();
            }
            if(!funTest.hasName(me,on))return on.toString();
            if(!funTest.hasNowPageNum(me,on))return on.toString();
            String name= funTest.getName(me);
            int num= funTest.getNowPageNum(me);
            on.put(enumCMKey.nowPageNum.name(), num);
            on.put(enumCMKey.allPageNum.name(), toolPetInfo.queryAllPageNum(name));
            List<entPetInfo> a=toolPetInfo.queryList(name,num);
            ArrayNode an=om.createArrayNode();
            for(entPetInfo p:a){
                ObjectNode qw=om.createObjectNode();
                qw.put(enumCMKey.pid.name(), p.getPid());
                qw.put(enumCMKey.name.name(),p.getName());
                qw.put(enumCMKey.attack.name(), p.getAttack());
                qw.put(enumCMKey.defence.name(), p.getDefence());
                qw.put(enumCMKey.blood.name(), p.getBlood());
                an.add(qw);
            }
            on.put(enumCMKey.data.name(), an);
            on.put(enumCMKey.status.name(), true);
            return on.toString();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/query/carry/num")
    public String queryCarryNum(@RequestBody reqData info){
        try{
            ObjectMapper om = new ObjectMapper();
            JsonNode me = om.readTree(info.getData());
            ObjectNode on=om.createObjectNode();
            on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
            on.put(enumCMKey.status.name(), false);
            if(!funTest.hasPhone(me,on))return on.toString();
            if(!funTest.hasCommand(me,on))return on.toString();
            String phone= funTest.getPhone(me);
            String command= funTest.getCommand(me);
            if(!toolAdminAccount.queryCommand(phone).equals(command)){
                on.put(enumCMKey.tip.name(), enumCMTip.error_command.getTip());
                return on.toString();
            }
            on.put(enumCMKey.carryNum.name(), funCarry.carryNum());
            on.put(enumCMKey.status.name(), true);
            return on.toString();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/query/carry/rob/num")
    public String queryCarryRobNum(@RequestBody reqData info){
        try{
            ObjectMapper om = new ObjectMapper();
            JsonNode me = om.readTree(info.getData());
            ObjectNode on=om.createObjectNode();
            on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
            on.put(enumCMKey.status.name(), false);
            if(!funTest.hasPhone(me,on))return on.toString();
            if(!funTest.hasCommand(me,on))return on.toString();
            String phone= funTest.getPhone(me);
            String command= funTest.getCommand(me);
            if(!toolAdminAccount.queryCommand(phone).equals(command)){
                on.put(enumCMKey.tip.name(), enumCMTip.error_command.getTip());
                return on.toString();
            }
            on.put(enumCMKey.carryRobNum.name(), funCarry.carryRobNum());
            on.put(enumCMKey.status.name(), true);
            return on.toString();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
